﻿using System.Collections.Generic;
using System.Linq;
using System.Xml;

namespace BuzzX8.Xmpp.StreamElements
{
    public class SaslFeature : StreamFeature
    {
        private ICollection<string> mechanisms;

        public SaslFeature() : base("mechanisms", XmppNamespaces.Sasl, true)
        {
            mechanisms = new List<string>();
        }

        public SaslFeature(ICollection<string> mechanisms) : this()
        {
            foreach (string mechanism in mechanisms) Mechanisms.Add(mechanism);
        }

        public ICollection<string> Mechanisms
        {
            get { return mechanisms; }
        }

        protected override void WritePayload(XmlWriter writer)
        {
            foreach (string mechanism in Mechanisms) writer.WriteElementString("mechanism", mechanism);
        }

        public override bool Equals(object obj)
        {
            if (!(obj is SaslFeature) && obj == null) return false;
            SaslFeature feature = (SaslFeature) obj;
            return feature.Mechanisms.SequenceEqual(Mechanisms);
        }
    }
}
